var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    shopData: [],
    searchShowed: false,
    searchVal: "",
    selected: false,
    index: '',
    currentPage: 1,
    pageNumber: 0,
    firstPageDisabled: true,
    prevPageDisabled: true,
    nextPageDisabled: false,
    lastPageDisabled: false
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName + "--选择堆场" });
    wx.removeStorage({
      key: 'path_shop',
      success: function (res) {
      }
    });
    // this.setData({
    //   index: e.index
    // });
    this.loadData();
  },
  onReady: function () {
    wx.hideToast()
  },
  showSearch: function () {
    this.setData({
      searchShowed: true
    });
  },
  hideSearch: function () {
    this.setData({
      searchVal: "",
      searchShowed: false
    });
    this.loadData();
  },
  clearSearch: function () {
    this.setData({
      searchVal: ""
    });
    this.loadData();
  },
  searchTyping: function (e) {
    this.setData({
      searchVal: e.detail.value
    });
    this.loadData();
  },
  loadData() {
    wx.showLoading({
      title: '加载数据中...',
    });
    var that = this;
    util.getData("/atapi/containeryard/containeryard", {
      CO_ID: app.globalData.CO_ID,
      currentPage: that.data.currentPage,
      sql: that.data.searchVal
    }, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success) {
        var _data = res.data.data[0], _data_ = [], rowNumber = Number(res.data.data[1][0].COUNT);
        that.setData({
          pageNumber: Math.ceil(rowNumber / 50)
        })
        that.pageDisabled();
        for (var i = 0; i < _data.length; i++) {
          var coord = String(_data[i].coord).split(",");
          _data_.push({ name: _data[i].name, address: _data[i].address, value: _data[i].ID });
        };
        that.setData({
          shopData: _data_,
          dataList: _data_
        })
      }
      wx.hideLoading();
    });
  },
  setDataList: function (text) {
    var that = this;
    var data = that.data.dataList, _data_ = [];
    for (var i = 0; i < data.length; i++) {
      if (String(data[i].name).indexOf(text) > -1 || String(data[i].address).indexOf(text) > -1 || String(data[i].ID).indexOf(text) > -1 ) {
        _data_.push({ name: data[i].name, address: data[i].address, value: data[i].value });
      }
    };
    that.setData({
      shopData: _data_
    })
  },
  dataSelect: function (e) {
    var shopData = this.data.shopData;
    for (var i = 0; i < shopData.length; i++) {
      shopData[i].checked = shopData[i].value == e.detail.value;
      if (shopData[i].value == e.detail.value) {
        shopData[i].index = this.data.index;
        wx.setStorage({
          key: "path_containeryard",
          data: shopData[i]
        });
        this.setData({
          selected: true
        })
      }
    }
    this.setData({
      shopData: shopData
    });
  },
  confirmData: function () {
    if (this.data.selected) {
      wx.navigateBack({
        delta: 1
      });
    } else {
      wx.showToast({
        title: "请选择堆场",
        image: "/images/wrong.svg",
        duration: 3000
      });
    }
  },
  prevPage: function () {
    var currentPage = Number(this.data.currentPage);
    if (currentPage > 1) {
      currentPage--;
    } else {
      currentPage = 1;
    }
    this.setData({
      currentPage: currentPage
    });
    this.pageDisabled();
    this.loadData();
  },
  nextPage: function () {
    var currentPage = Number(this.data.currentPage), pageNumber = this.data.pageNumber;
    if (currentPage < pageNumber) {
      currentPage++;
    } else {
      currentPage = pageNumber;
    }
    this.setData({
      currentPage: currentPage
    });
    this.pageDisabled();
    this.loadData();
  },
  firstPage: function () {
    this.setData({
      currentPage: 1
    });
    this.pageDisabled();
    this.loadData();
  },
  lastPage: function () {
    this.setData({
      currentPage: this.data.pageNumber
    });
    this.pageDisabled();
    this.loadData();
  },
  pageDisabled: function () {
    var currentPage = Number(this.data.currentPage), pageNumber = this.data.pageNumber;
    if (pageNumber == 1) {
      this.setData({
        firstPageDisabled: true,
        prevPageDisabled: true,
        nextPageDisabled: true,
        lastPageDisabled: true
      });
    } else if (currentPage == 1) {
      this.setData({
        firstPageDisabled: true,
        prevPageDisabled: true,
        nextPageDisabled: false,
        lastPageDisabled: false
      });
    } else if (currentPage < pageNumber) {
      this.setData({
        firstPageDisabled: false,
        prevPageDisabled: false,
        nextPageDisabled: false,
        lastPageDisabled: false
      });
    } else if (currentPage == pageNumber) {
      this.setData({
        firstPageDisabled: false,
        prevPageDisabled: false,
        nextPageDisabled: true,
        lastPageDisabled: true
      });
    }
  },
  cancelConfirmData: function () {
    wx.navigateBack({
      delta: 1
    });
  }
});